// pages/applyRepair/repairList.js
const app = getApp();
Page({
  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '维修记录',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333;background-color: #f1f1f1;",
    navs:[
      {title: "首页",class:"", path:"/pages/index/index", icon: "", font: "fa fa-home"},
      {title: "信息",class:"", path:"", icon: "", font: "fa fa-comments-o"},
      {title: "维修",class:"", path:"", icon: "", font: "fa fa-wrench"},
      {title: "我的",class:"co", path:"/pages/mine/mine", icon: "", font: "fa fa-user-circle"},
    ],
    userInfo: {
      nickName: "",
      addr: "",
      phone: "",
      avatar: ""
    },
    contents:[],
    positions:[],
    orderList:[],
    hasChange: false,
    ready: false
  },
  onLoad: function (options) {
  },
  onReady: function () {
  },
  onShow: function () {
    let v = this;
    if (app.globalData.token){
      // v.getPosition();
      v.getFixOption();
    } else {
      console.log("app.doLogin");
      app.doLogin(v,v.getPosition);
    }
  },
  getFixOption() {
    let v = this;
    let url = app.globalData.host + '/api/common/get_fix_options';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data.position || [];
          let i = r.data.content || [];
          console.log("position",l);
          v.setData({positions: l, contents: i});
          v.getHistory();
        }
      }
    })
  },
  getPosition() {
    let v = this;
    let url = app.globalData.host + '/api/common/fix_position';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data || [];
          console.log("position",l);
          v.setData({positions: l});
          v.getHistory();
        }
      }
    })
  },
  getHistory(){
    let v = this;
    v.setData({hasChange: false});
    let url = app.globalData.host + '/api/fix/list';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data.list || [];
          let ll = v.data.positions;
          for(let k in l) {
            let position = '';
            for(let i in ll) if(ll[i].value == l[k].position) position = ll[i].name;
            l[k].positions = position;
          }
          console.log("getHistory",l);
          v.setData({orderList: l});
        }
      }
    })
  },
  viewInfo(ev) {
    let v = this;
    console.log("viewInfo",ev)
    let ds = ev.target.dataset;
    if(!ds) return;
    let index = ds.index;
    if(v.data.orderList[index]) {
      app.globalData.repairInfo = v.data.orderList[index];
      wx.navigateTo({ url: "/pages/applyRepair/repairInfo"});
    }
  },
  subComment(ev) {
    let v = this;
    let ds = ev.target.dataset;
    if(!ds || !ds.id) return;
    let fix_id = ds.id;
    let comment = '';
    app.globalData.comment_fix_id = fix_id;
    if (fix_id) {
      wx.navigateTo({  url: '/pages/applyRepair/repairComment',});
      return;
    }
    
    wx.showModal({
      title: '填写评价',
      content: '非常棒！！',
      editable: true,
      success (res) {
        console.log(res)
        if (res.confirm) {
          comment = res.content;
          v.doSubComment(fix_id,comment);
        }
      }
    });
  },
  doSubComment(fix_id,comment) {
    let v = this;
    let url = app.globalData.host + '/api/fix/comment';
    wx.request({
      url: url, data: {fix_id:fix_id,content:comment, token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          wx.showToast({ title: r.msg || '提交成功',icon:'none'})
        } else {
          wx.showToast({ title: r.msg || '提交失败',icon:'none'})
        }
      }
    })
  },
  requestTicket(ev){
    let v = this;
    let ds = ev.target.dataset;
    if(!ds || !ds.id) return;
    let fix_id = ds.id;
    wx.showModal({
      title: '二次维修',
      content: '需要申请二次维修？',
      success (res) {
        console.log(res)
        if (res.confirm) {
          // comment = res.content;
          v.requestRepair(fix_id);
        }
      }
    });
  },
  requestRepeat(ev){
    let v = this;
    let ds = ev.target.dataset;
    if(!ds || !ds.id) return;
    let fix_id = ds.id;
    console.log("requestRepeat", fix_id);
    let l = v.data.orderList;
    let info = null;
    for(let k in l) if (l[k].fix_id == fix_id) info = l[k];
    if (info) {
      console.log("requestRepeat", info);
      app.globalData.repeatInfo = info;
      wx.navigateTo({
        url: '/pages/applyRepair/repeatRepair',
      })
    }
  },
  requestRepair(fix_id){
    console.log("requestTicket repeat", fix_id);
    let url = app.globalData.host + '/api/fix/repeat';
    wx.request({
      url: url, data: {fix_id:fix_id, token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          wx.showToast({ title: r.msg || '申请成功',icon:'none'});
          v.getHistory();
        } else {
          wx.showToast({ title: r.msg || '申请失败',icon:'none'})
        }
      }
    })
  },
  onSuccess: function (res) {
    console.log(res.detail);
  },
  onFail: function (res) {
    console.log(res);
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },
  onShareAppMessage: function () {
  }
})